var time = require("../../../../utils/util.js");
var app = getApp();
Page({
  data: {
    message: {},
    page: 1,
    value: "",
  },
  onShow: function(e) {
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/adds/informationlist',
      data: {
        openid: openid,
        page: 1
      },
      method: "post",
      success(res) {
        console.log(res)
        for (var i = 0; i < res.data.data.length; i++) {
          //字符串
          if (res.data.data[i].content.length > 30) {
            res.data.data[i].content = res.data.data[i].content.substring(0, 30) + '...'
          }
        }
        that.setData({
          code: res.data.code,
          message: res.data.data
        })
      }
    })
  },
  //搜索
  zixunInput: function(e) {
    var openid = wx.getStorageSync("openid")
    var that = this;
    this.setData({
      value: e.detail.value
    })
    wx.request({
      url: 'https://www.mamingwang.cn/api/adds/informationsee',
      data: {
        openid: openid,
        text: e.detail.value
      },
      method: "post",
      success(res) {
        console.log(res)
        if (res.data.data) {
          for (var i = 0; i < res.data.data.length; i++) {
            //字符串
            if (res.data.data[i].title.length > 10) {
              res.data.data[i].title = res.data.data[i].title.substring(0, 10) + '...'
            }
            if (res.data.data[i].content.length > 30) {
              res.data.data[i].content = res.data.data[i].content.substring(0, 30) + '...'
            }
            //时间
            var d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
            res.data.data[i].createtime = time.timeHandle(d);
          }
          that.setData({
            message: res.data.data
          })
        } else {
          that.setData({
            message: []
          })
        }
      }
    })
  },
  //跳转到详情页面
  codeFn: function(e) {
    wx.navigateTo({
      url: 'xiangQing/xiangQing?zid=' + e.currentTarget.dataset.zid,
    })
  },
  //下拉刷新数据
  onPullDownRefresh: function() {
    var openid = wx.getStorageSync('openid')
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/adds/informationlist',
      data: {
        openid: openid,
        page: 1
      },
      method: "post",
      success(res) {
        console.log(res)
        for (var i = 0; i < res.data.data.length; i++) {
          //字符串
          if (res.data.data[i].type == 2) {
            if (res.data.data[i].content.length > 24) {
              res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
            }
          }
          //时间
          var d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
          res.data.data[i].createtime = time.timeHandle(d);
        }
        that.setData({
          message: res.data.data,
          value: "",
          page: 1
        })
        wx.stopPullDownRefresh()
      }
    })
  },
  //上拉加载更多
  onReachBottom: function() {
    var openid = wx.getStorageSync('openid')
    var that = this;
    this.setData({
      page: this.data.page + 1
    })
    wx.request({
      url: 'https://www.mamingwang.cn/api/adds/informationlist',
      data: {
        openid: openid,
        page: this.data.page
      },
      method: "post",
      success(res) {
        console.log(res);
        if (res.data.data) {
          for (var i = 0; i < res.data.data.length; i++) {
            //字符串
            if (res.data.data[i].type == 2) {
              if (res.data.data[i].content.length > 24) {
                res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
              }
            }
            //时间
            var d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
            res.data.data[i].createtime = time.timeHandle(d);
            //添加到已有数组message
            that.data.message.push(res.data.data[i])
          }
          that.setData({
            message: that.data.message,
          })
        } else {
          wx.showToast({
            title: '暂无更多信息',
            icon: "none",
            duration: 1500
          })
        }
      }
    });
  }
})